25#include "yaml-cpp/yaml.h"
53 using map<char, T>::map;
62 const std::string& _field,
64 const std::string& _keys =
ALL_KEYS);
72 bool has(
char _key)
const;
86 for (
auto&& pair : _x)
88 _os << pair.first <<
": " << pair.second << std::endl;
106 const std::string& _field,
107 const bool _required,
108 const std::string& _keys)
114 throw std::runtime_error(
"Composite: constructor with a non-map yaml node");
119 for (
auto key : _keys)
120 if (not _n[key] and _required)
122 throw std::runtime_error(std::string(
"Composite ") +
typeid(T).name() +
123 " constructor: Missing node for key '" + key +
"'.");
127 for (
auto node_pair : _n)
133 key = node_pair.first.as<
char>();
135 catch (
const std::exception& e)
137 throw std::runtime_error(std::string(
"In Composite ") +
typeid(T).name() +
138 " constructor: There is a key of type different than char: " +
139 node_pair.first.as<std::string>() +
". Error: " + e.what());
144 _keys.find(key) == std::string::npos)
148 if (not _field.empty())
150 if (node_pair.second[_field])
154 this->emplace(key, node_pair.second[_field].as<T>());
156 catch (
const std::exception& e)
159 em << node_pair.second[_field];
160 throw std::runtime_error(
"In Composite constructor: Failed to emplace the entry " + _field +
161 " for key '" + key +
"' with error: " + e.what() +
"\nNode:\n" +
168 em << node_pair.second;
169 throw std::runtime_error(
"In Composite constructor: Missing mandatory field '" + _field +
170 "' for key '" + key +
"'\nNode:\n" + em.c_str());
177 this->emplace(key, node_pair.second.as<T>());
179 catch (
const std::exception& e)
182 em << node_pair.second;
183 throw std::runtime_error(
"In Composite constructor: Failed to emplace the entry for key '" +
184 std::string(1, key) +
"' with error: " + e.what() +
"\nNode:\n" +
192 WOLF_WARN(
"The YAML node is not defined. Constructing an empty composite");
199 return this->count(_key) != 0;
205 for (
auto&& key : _query_keys)
219 for (
auto&& pair : *
this) keys.push_back(pair.first);
227 if (not this->has(_keys))
229 throw std::runtime_error(
"Composite<T>::operator() required keys " + _keys +
" are not available, only have " +
234 for (
auto key : _keys)
236 output.emplace(key, this->at(key));
244 return YAML::Node(*
this);
255struct convert<
wolf::Composite<T>>
262 for (
auto&& pair : composite)
264 n[pair.first] = pair.second;
Map of state keys (char) to objects of type T (vectors, matrices, types, priors, ....
Definition: composite.h:49
Composite(const YAML::Node &_n, const std::string &_field, const bool _required, const std::string &_keys=ALL_KEYS)
Constructor from a YAML map of keys.
Definition: composite.h:105
StateKeys getKeys() const
Returns all the keys.
Definition: composite.h:216
bool has(const StateKeys &_structure) const
Returns whether the composite has all the keys.
Definition: composite.h:203
T CompositeType
Type of the stored values.
Definition: composite.h:51
virtual ~Composite()=default
Destructor.
Composite operator()(const std::string &_keys) const
Returns the composite of the given keys. Throws if any is missing.
Definition: composite.h:225
friend std::ostream & operator<<(std::ostream &_os, const Composite< T > &_x)
Prints the composite to a stream.
Definition: composite.h:84
YAML::Node toYaml() const
Returns a YAML map of the keys.
Definition: composite.h:242
bool has(char _key) const
Returns whether the composite has a key.
Definition: composite.h:197
#define WOLF_WARN(...)
Definition: logging.h:511
Definition: composite.h:251
Definition: capture_base.h:23
std::string StateKeys
Definition: composite.h:31
static std::string ALL_KEYS
Definition: composite.h:33
Composite< bool > BoolComposite
Definition: composite.h:102
StateKeys keysIntersection(const StateKeys &_keys1, const StateKeys &_keys2)
Intersection of two sets of keys: the keys of _keys1 that are also in _keys2, in the order of _keys1 ...
Definition: composite.cpp:31
StateKeys keysUnion(const StateKeys &_keys1, const StateKeys &_keys2)
Union of two sets of keys: the keys of _keys1 followed by the keys of _keys2 not in _keys1 (no duplic...
Definition: composite.cpp:23
static Node encode(const wolf::Composite< T > &composite)
Encodes a composite into a YAML node.
Definition: composite.h:259
static bool decode(const Node &node, wolf::Composite< T > &composite)
Decodes a composite from a YAML node.
Definition: composite.h:272